package com.zhika.zishi.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhika.zishi.db.datas.UserData;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/5/4.
 */
public class OperateDb {

    private DbHelper dbHelper;

    public OperateDb(Context context) {
        dbHelper = DbHelper.getIntance(context);
    }

    /**
     * 给用户的表添加数据
     *
     * @param user
     */
    public void insertUser(UserData user) {

        //如果user是null,就退出此方法
        if (user == null) {
            return;
        }

        //如果user表中存在该用户
        if (isExistUser(user.getUserId())) {
            return;
        }

        //如果该表中没有此用户，或者该表是空的，就插入此用户
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DbHelper.COLUMN_USER_ID, user.getUserId());
        values.put(DbHelper.COLUMN_USER_NICKNAME, user.getNickname());
        values.put(DbHelper.COLUMN_USER_TOKEN, user.getUserToken());
        values.put(DbHelper.COLUMN_IS_LOGIN, user.getIsLogin());

        database.insert(DbHelper.TABLE_USER, null, values);
        if (database.isOpen()) {
            database.close();
        }
    }

    /**
     * 查询用户表
     *
     * @return
     */
    public ArrayList<UserData> queryUser() {

        //存放查询到的用户
        ArrayList<UserData> userList = new ArrayList<>();

        SQLiteDatabase database = dbHelper.getWritableDatabase();
        Cursor cursor = database.query(DbHelper.TABLE_USER, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                UserData userData = new UserData();
                int uid = cursor.getInt(cursor.getColumnIndex(DbHelper.COLUMN_USER_ID));
                userData.setUserId(uid);

                String nickname = cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_USER_NICKNAME));
                userData.setNickname(nickname);

                String token = cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_USER_TOKEN));
                userData.setUserToken(token);

                int isLogin = cursor.getInt(cursor.getColumnIndex(DbHelper.COLUMN_IS_LOGIN));
                userData.setIsLogin(isLogin);

                userList.add(userData);

            } while (cursor.moveToNext());
        }

        if (database.isOpen()) {
            database.close();
        }
        return userList;
    }

    /**
     * 更新User表中的token的值
     */
    public void updateUserToken(UserData userData) {

        //如果userData是空的，退出
        if (userData == null) {
            return;
        }

        //如果存在，则修改
        if (isExistUser(userData.getUserId())) {
            SQLiteDatabase database = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(DbHelper.COLUMN_USER_TOKEN, userData.getUserToken());
            database.update(DbHelper.TABLE_USER, values, DbHelper.COLUMN_USER_ID + " = ?", new String[]{String.valueOf(userData.getUserId())});

            if (database.isOpen()) {
                database.close();
            }

            //1--已登录
            updateIsLoginState(userData.getUserId(),1);
        }
        //不存在，则插入
        else {
            insertUser(userData);
        }
    }

    /**
     * 判断User表中是否存在该用户
     *
     * @param uid 用户的id
     * @return
     */
    private boolean isExistUser(long uid) {
        //得到数据库中user表中的数据
        ArrayList<UserData> userList = queryUser();

        if (userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                if (uid == userList.get(i).getUserId()) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 更改uid用户的登录状态
     *
     * @param uid
     */
    public void updateIsLoginState(long uid,int isLogin) {

        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        //2 -- 已退出，1-- 已登录
        values.put(DbHelper.COLUMN_IS_LOGIN, isLogin);
        database.update(DbHelper.TABLE_USER, values, DbHelper.COLUMN_USER_ID + " = ?", new String[]{String.valueOf(uid)});

        if (database.isOpen()) {
            database.close();
        }
    }
}
